package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.LogStatisFacade;
import com.qding.park.manager.pojo.LogInfo;
import com.qding.park.manager.pojo.example.NcPresenExample;
import com.qding.park.manager.pojo.noncore.NcMonthRent;

@Service("logStatisFacade")
public class LogStatisFacadeImpl implements LogStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(LogStatisFacadeImpl.class); 
	
	@Autowired
	private  MongoTemplate mongoTemplate;

	@Override
	public ParkDataResult operationLoglist(NcPresenExample example,int jumpPage, int pageLength) throws BaseException{
		
		String tablename_operlog = MongodbConstant.TABLENAME_OPERATIONLOG;
		
		Criteria matchCrieria = new Criteria();
		if (example.getStartTime() != null && example.getEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("createDate").gte(example.getStartTime()),
					Criteria.where("createDate").lte(example.getEndTime()));
		} else if (example.getStartTime()!= null) {
			matchCrieria.andOperator(Criteria.where("createDate").gte(example.getStartTime()));
		} else if (example.getEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("createDate").lte(example.getEndTime()));
		}
		
		Query query=Query.query(matchCrieria);
		query.with(new Sort(new Order(Direction.DESC,"createDate")));
		query.skip((jumpPage - 1) * pageLength);
		query.limit(pageLength);
		
		Query querytotal=Query.query(matchCrieria);
		
		List<LogInfo> logInfolist= mongoTemplate.find(query, LogInfo.class, tablename_operlog);
		
		long total= mongoTemplate.count(querytotal, tablename_operlog);
		
		List dataList = new ArrayList();
		for(LogInfo loginfo:logInfolist){
			Map<String, Object> map = bean2map(loginfo);
			dataList.add(map);
		}
		
		ParkDataResult parkDataResult=ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("currentPage", jumpPage);
		parkDataResult.putDataOnObj("numPerPage", pageLength);
		parkDataResult.putDataOnObj("totalCount", total);
		parkDataResult.putDataOnObj("hast", jumpPage > 1 ? true : false);
		parkDataResult.putDataOnObj("next", jumpPage * pageLength < total ? true : false);
		parkDataResult.putDataOnObj("list",dataList);
		return parkDataResult;
	}
	
	
	private  Map<String, Object> bean2map(LogInfo loginfo){
		Map<String, Object> map = new HashMap<String, Object>();
		if(loginfo==null){
			return map;
		}
		map.put("createDate", loginfo.getCreateDate()==null?0:loginfo.getCreateDate().getTime());
		map.put("remark", loginfo.getDescription());
		map.put("requestUri", loginfo.getRequestUri());
		map.put("type", loginfo.getType());
		map.put("userName", loginfo.getUserName());
		
		return map;
	}


    @Override
    public void save(LogInfo log) throws BaseException {
        String tablename= MongodbConstant.TABLENAME_OPERATIONLOG;
        mongoTemplate.insert(log,tablename);
        
    }
}
